package com.example.mytips.utils;

import com.intellij.openapi.ui.DialogWrapper;

import java.awt.*;

public class DialogUtils {

    ///在屏幕中央
    public static void screenCenterShow(DialogWrapper dialogWrapper){
        ///如果电脑连接了多台显示器，则在当前操作的显示器上弹出弹窗
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsDevice[] screens = ge.getScreenDevices();

        // 获取当前鼠标所在的屏幕
        PointerInfo pointerInfo = MouseInfo.getPointerInfo();
        GraphicsDevice currentScreen = pointerInfo.getDevice();

        // 计算DialogWrapper在当前屏幕上的位置
        for (GraphicsDevice screen : screens) {
            if (screen.equals(currentScreen)) {
                GraphicsConfiguration config = screen.getDefaultConfiguration();
                Rectangle bounds = config.getBounds();
                int x = bounds.x + (bounds.width - dialogWrapper.getSize().width) / 2;
                int y = bounds.y + (bounds.height - dialogWrapper.getSize().height) / 2;
                dialogWrapper.setLocation(x, y); // 将DialogWrapper位置设置为当前屏幕中央
                break;
            }
        }
    }
}
